package cn.mapper;

import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.Map;

public class WorkOrderProvider {

    public String selectPage(Map<String,Object> map){
        return new SQL(){{
            SELECT("w.*,su.name user_name,suu.name transport_name,suuu.name recipient_name,office.name company_name");
            FROM("work_order w");
            LEFT_OUTER_JOIN("sys_user su on w.create_user_id =su.id " +
                    "LEFT join sys_user suu on w.transport_user_id = suu.id " +
                    "left join sys_user suuu on w.recipient_user_id =suuu.id " +
                    "left join sys_office office on su.company_id = office.id ");
            WHERE("w.del_flag = 0");
            if (map.containsKey("status")&& !StringUtils.isEmpty(map.get("status"))){
                WHERE("w.STATUS = #{status}");
            }
            if (map.containsKey("startDate")&& !StringUtils.isEmpty(map.get("startDate"))){
                WHERE("w.create_date >= #{startDate}");
            }
            if (map.containsKey("endDate")&& !StringUtils.isEmpty(map.get("endDate"))){
                WHERE("w.create_date <= #{endDate}");
            }
        }}.toString();
    }

    public String showDetail(Long id){
        return new SQL(){{
            SELECT("w.`code`,w.`status`,su.NAME user_name,so.`name` company_name,suu.NAME transport_name,suuu.NAME recipient_name,su.phone ");
            FROM("work_order w");
            LEFT_OUTER_JOIN("sys_user su ON w.create_user_id = su.id " +
                    " LEFT JOIN sys_user suu ON w.transport_user_id = suu.id " +
                    " LEFT JOIN sys_user suuu ON w.recipient_user_id = suuu.id " +
                    " LEFT JOIN sys_office so ON su.company_id = so.id ");
            WHERE("w.del_flag =0 and w.id = #{id} ");
        }}.toString();
    }
}
